Questions
44 of 50
1What is a pseudo-class in CSS?
2How are pseudo-classes different from pseudo-elements?
3What is the syntax of a pseudo-class in CSS?
4Give some commonly used pseudo-classes.
5What does the :hover pseudo-class do?
6What’s the purpose of the :active pseudo-class?
7How does the :visited pseudo-class work for links?
8What is the :focus pseudo-class used for?
9What does the :checked pseudo-class do?
10How does the :disabled pseudo-class behave?
11What is the difference between :first-child and :first-of-type?
12How do :nth-child() and :nth-of-type() differ?
13How do you select every odd or even element using pseudo-classes?
14How can you style an element when it’s being hovered over along with its child?
15What does the :not() pseudo-class do?
16How can you use multiple pseudo-classes together?
17What is the difference between :link and :visited?
18What does the :target pseudo-class represent?
19How can you use :empty to detect empty elements?
20How does the :root pseudo-class differ from the html selector?
21What does the :valid and :invalid pseudo-class do?
22How can you use :required and :optional pseudo-classes in forms?
23What is the use of :in-range and :out-of-range?
24What does :read-only and :read-write do?
25How can you style an input field when it’s autofilled?
26How does the :focus-within pseudo-class work?
27What’s the difference between :focus and :focus-visible?
28What is the purpose of the :placeholder-shown pseudo-class?
29How can you style a checkbox when it is checked or unchecked?
30How can you style invalid form inputs without JavaScript?
31Can pseudo-classes be chained together? Give an example.
32What is the specificity of pseudo-classes compared to normal selectors?
33How can you use :not() effectively to exclude elements from styling?
34How does :has() pseudo-class work, and why is it powerful?
35Is :has() supported in all browsers?
36How does :is() differ from :where() in terms of specificity?
37How can you combine :is() or :where() with other selectors for cleaner code?
38What’s the difference between :nth-child() and :nth-last-child()?
39How can you style only the last element of a list using pseudo-classes?
40What does the :lang() pseudo-class do?
41How do you change a button’s color when hovered but not when disabled?
42How can you highlight the current section in a menu using :target?
43How can you style a form field differently when focused and valid?
44How do you style every third list item differently?
45How can you style alternate table rows without adding extra classes?
46How do you hide empty <p> tags using pseudo-classes?
47How can you style the parent when any child inside it is focused?
48How can you highlight a link only when it’s both focused and hovered?
49How can you style the first letter of only the first paragraph using pseudo-classes?
50How would you use :has() to select a <div> that contains an image?
44 / 50

How do you style every third list item differently?

Styling Every Third List Item Using :nth-child() in CSS

You can use the :nth-child() pseudo-class in CSS to style elements based on their order within a parent. To target every third list item, you can use the pattern 3n inside :nth-child().

Key Points About :nth-child()
  1. 1

    :nth-child() selects elements based on their position within a parent element.

  2. 2

    You can use formulas like 2n, 3n, or 3n+1 to create repeating patterns.

  3. 3

    li:nth-child(3n) targets every third list item in a list.

Example: Styling Every Third List Item

In this example, the :nth-child(3n) selector highlights every third list item with a blue background and bold text. You can adjust the number to style elements in other repeating sequences.

Best Practices
  1. 1

    Use :nth-child() for pattern-based styling instead of adding extra classes.

  2. 2

    Remember that counting starts at 1, not 0.

  3. 3

    Use :nth-of-type() when you only want to target specific element types among mixed children.

  4. 4

    Combine with transitions for smooth visual effects.

Difficulty: 3/10
Topics: nth-child selector, CSS specificity, list item styling

Scenario Questions

0-2 years experience
  1. 1

    How would you style every third list item in an unordered list to have a different background color?

  2. 2

    What happens if you use :nth-child(3) instead of :nth-child(3n)? Why doesn't that work for every third item?

2-5 years experience
  1. 1

    Our product page shows a list of featured items, but after adding a promotional banner between items, every third item stopped getting the highlight style — what’s likely broken and how would you fix it?

  2. 2

    A designer wants every third list item to have a border, but the list is rendered conditionally — sometimes it’s empty or has only 2 items. How do you ensure the styling is robust and doesn’t cause layout shifts?

5-8 years experience
  1. 1

    We’re building a scalable component library where lists can be nested arbitrarily — how would you design a CSS rule to style every third item without affecting nested lists or breaking with dynamic content injection?

  2. 2

    A performance audit showed CSS reflows on list updates. How would you optimize the styling of every third item to minimize layout thrashing in a high-frequency update scenario?

8+ years experience
  1. 1

    We’re migrating a legacy app with hundreds of hardcoded list styles to a CSS-in-JS system — how do you ensure the 'every third item' pattern is preserved consistently across teams without introducing specificity wars or runtime performance penalties?

  2. 2

    Our design system supports internationalization and dynamic content lengths — how would you architect the CSS strategy for this pattern to remain maintainable across 50+ product teams with different styling conventions and deployment cycles?

Follow-up Questions

  • What if the list items are dynamically generated by a framework like React — does your selector still work?
  • How would you debug if every third item isn't styling as expected?
  • What happens if you have nested <ul> elements inside your list?